﻿using System;
using NHibernate;

namespace Framework.Repository.Testing
{
    public sealed class IntegrationTest : IDisposable
    {
        private readonly ISessionFactory sessionFactory;

        public IntegrationTest(IAutoMappingTypeProvider autoMappingTypeProvider)
            : this(new SqlitePersistence(), autoMappingTypeProvider)
        {

        }

        public IntegrationTest(IPersistence persistence, IAutoMappingTypeProvider autoMappingTypeProvider)
        {
            var sessionFactoryBuilder = new SessionFactoryBuilder(autoMappingTypeProvider, persistence);
            sessionFactory = sessionFactoryBuilder.Build();
        }

        public IRepository<T> CreateRepository<T>() where T : class
        {
            return new Repository<T>(sessionFactory);
        }

        public void Dispose()
        {
            sessionFactory.Dispose();
        }
    }
}